package com.softer.wxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.shiro.ShiroUtil;
import com.softer.wxzj.common.util.OperUtil;
import com.softer.wxzj.common.util.StringUtils;
import com.softer.wxzj.entity.SysOperator;
import com.softer.wxzj.entity.SysOperatorOuterNet;
import com.softer.wxzj.mapper.SysOperatorMapper;
import com.softer.wxzj.mapper.SysOperatorOuterNetMapper;
import com.softer.wxzj.service.SysOperatorOuterNetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2020/8/12
 */
@Service
public class SysOperatorOuterNetServiceImpl implements SysOperatorOuterNetService {
    @Autowired
    private SysOperatorOuterNetMapper sysOperatorOuterNetMapper;
    @Autowired
    private OperUtil zoneCodeUtil;
    @Autowired
    private SysOperatorServiceImpl sysOperatorService;
    @Autowired
    private SysOperatorMapper sysOperatorMapper;

    /**
     * 查询机构主账户列表
     *
     * @param page                分页实体
     * @param sysOperatorOuterNet 查询实体
     * @return 主账户列表
     */
    @Override
    public PageInfo<SysOperatorOuterNet> getMainOperatorList(Page page, SysOperatorOuterNet sysOperatorOuterNet) {
        sysOperatorOuterNet.setLoginZoneCode(zoneCodeUtil.getZoneCode());
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<SysOperatorOuterNet> list = sysOperatorOuterNetMapper.getMainOperatorList(sysOperatorOuterNet);
        return new PageInfo<>(list);
    }

    /**
     * 修改账户
     *
     * @param sysOperator 账户信息
     * @return 修改结果
     */
    @Override
    public CommonResult update(SysOperator sysOperator) {
        String operPwd = sysOperator.getOperPwd();
        if (StringUtils.isNotEmpty(operPwd)) {
            String salt = ShiroUtil.createSalt();
            String passWord = ShiroUtil.createPassWord(sysOperatorService.getPassWord(sysOperator.getOperPwd()), salt);
            sysOperator.setSalt(salt);
            sysOperator.setOperPwd(passWord);
        }
        int i = sysOperatorMapper.update(sysOperator);
        return CommonResult.ResponseResult(i);
    }
}
